package br.com.sgo.service;

import br.com.caelum.vraptor.ioc.Component;
import br.com.sgo.dao.UserDao;
import br.com.sgo.exception.LoginException;
import br.com.sgo.model.User;
import br.com.sgo.model.UserSession;

@Component
public class LoginService {
	
	private final UserSession userSession;
	private final UserDao userDao;
	
	public LoginService(UserSession userSession, UserDao userDao) {
		this.userSession = userSession;
		this.userDao = userDao;
	}
	
	public void login(User user) throws Exception {
		
		user = userDao.login(user);
		if(user == null)
			throw new LoginException("User not found!");
		
		userSession.setUser(user);
	}
	
	public boolean hasLoggedUser() {
		return userSession.getUser() != null;
	}
	
	public void logout() {
		
		userSession.setUser(null);
	}	

}
